<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of IndexController
 *
 * @author minhhq
 */
class Tab_IndexController extends MKT_Controller_Action {
    
    private $_id_social_page;
    private $_start_time;
    private $_end_time;
    private $_page_info;
    
    public function init() {
        parent::init();
        $this->_helper->layout->setLayout("tab_layout");
        $id_social_page = $this->_facebook->getIdPage();
        if (empty($id_social_page)) {
            $id_social_page = $this->_getParam("pid");
        }
        $this->_id_social_page = $id_social_page;
             
        $page_db = new Core_Model_DbTable_Pages();
        $page_info = $page_db->getPageInfo($id_social_page)->toArray();
        $this->_page_info = $page_info;
       
        $this->view->id_social_page = $id_social_page;
        $this->view->page_info = $page_info;
        
        //process time section
        $time = $this->_getParam("time", time());
        if ($page_info['reset_period'] == 'w') {
            $this->_start_time = MKT_Util::getFirstDayOfWeek($time);
            $this->_end_time = MKT_Util::getLastDayOfWeek($time);
            $this->view->can_vote = date('W') == date('W', $time);
        } else {
            $this->_start_time = MKT_Util::getFirstDayOfMonth($time);
            $this->_end_time = MKT_Util::getLastDayOfMonth($time);
            $this->view->can_vote = date('m') == date('m', $time);
        }
        
        $this->view->start_time = $this->_start_time;
        $this->view->end_time = $this->_end_time;
    }
    public function indexAction() {
        $user_action_db = new Core_Model_DbTable_TotalActions();
        
        $fans = $user_action_db->getTopFans($this->_id_social_page, $this->_start_time, $this->_end_time);
        
        $this->view->id_social_page = $this->_id_social_page;
        $this->view->offset = FAN_PER_PAGE;
        
        $id_social_user = $this->_facebook->getUser();
        if ($id_social_user) {
            $this->view->id_social_user = $id_social_user;
        } else {
            $this->view->id_social_user = NULL;
        }
        
        $this->_checkVoted($id_social_user, $fans);
        $this->view->fans = $fans;
    }
    
    public function viewdetailAction() {
        $this->_helper->layout()->setLayout("layout");
        
        $id_social_user = $this->_getParam("uid");
        
        $user_action_db = new Core_Model_DbTable_UserActions();
        $actions = $user_action_db->getactions($this->_id_social_page, $id_social_user, $this->_start_time, $this->_end_time);
        
        $total_action_db = new Core_Model_DbTable_TotalActions();
        $total_action    = $total_action_db->getUserTotalAction($id_social_user, $this->_start_time, $this->_end_time);

        $this->view->id_social_user = $id_social_user;
        $this->view->actions        = $actions;
        $this->view->total_action   = $total_action;
        $this->view->offset         = ACTION_PER_PAGE;
        $this->view->rank           = $this->_getParam("rank", 1);
    }
    
    private function _checkVoted($id_social_user, &$fans) {
        $voted_ids = array();
        foreach ($fans as $fan) {
            $voted_ids[] = $fan['id_social_user'];
        }
        
        $user_action_db = new Core_Model_DbTable_UserActions();
        $voted_result = $user_action_db->checkVoted($this->_id_social_page, $id_social_user, $voted_ids, $this->_start_time, $this->_end_time);
        
        foreach ($fans as &$fan) {
            $fan['voted'] = $voted_result[$fan['id_social_user']];
        }
    }
}

?>
